package com.windystory.wslibrary.base.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.Nullable;

import android.transition.Fade;

import com.windystory.wslibrary.R;
import com.windystory.wslibrary.base.app.EventBusCode;
import com.windystory.wslibrary.net.netstatus.INETWork;
import com.windystory.wslibrary.net.netstatus.NetWorkChange;
import com.windystory.wslibrary.util.ToastUtil;
import com.windystory.wslibrary.util.event.Event;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

/**
 * BaseActivity
 * Created by windystory on 12/12/2016.
 */
public abstract class BaseActivity extends SwipeBackActivity implements INETWork {
    private ProgressDialog mProgressDialog;
    public INETWork inetWork;
    private NetWorkChange netReceiver;
    public String TAG = "BaseActivity";

    /**
     * 初始化布局
     * public int getLayoutRes() {return R.layout.activity_read_book;}
     */
    public abstract int getLayoutRes();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onSubscribeEventBus();
        setContentView(R.layout.layout_skeleton);
        setContentView(getLayoutRes());
        initBaseActivity();
        initView();
        initData();
        initListener();
        setupWindowAnimation();
        setSwipeBackEnable(setEnableSwipeBack());
    }

    /**
     * 初始化BaseActivity
     */
    protected void initBaseActivity() {
        inetWork = this;
        netReceiver = new NetWorkChange();
        registerNetWorkReceiver();
    }

    /**
     * 设置是否测滑退出
     *
     * @return
     */
    protected abstract boolean setEnableSwipeBack();

    /**
     * 初始化View 可以在这里写或者用黄油刀
     */
    protected abstract void initView();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 初始化事件
     */
    protected abstract void initListener();


    /**
     * 初始 rxSubscription
     *
     * @author windystory
     * @date 2016-7-28
     */
    private void onSubscribeEventBus() {
        EventBus.getDefault().register(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusSubscribe(Event eventConfig) {
        switch (eventConfig.what) {
            case EventBusCode.EVENT_NETWORK_UNKNOWN:
                inetWork.withOutNetWork();
                break;
            case EventBusCode.EVENT_NETWORK_WIFI:
                inetWork.withWiFiNetWork();
                break;
            case EventBusCode.EVENT_NETWORK_MOBILE:
                inetWork.withMobileNetWork();
                break;
        }
    }

    /**
     * 显示ProgressDialog
     *
     * @param cancelable 是否可以点击取消
     * @param message
     */
    public void showProgress(boolean cancelable, String message) {
        if (mProgressDialog == null) {
            Drawable drawable = getResources().getDrawable(R.drawable.pic_progressbar_bg);
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setIndeterminateDrawable(drawable);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            mProgressDialog.setCancelable(cancelable);
            mProgressDialog.setCanceledOnTouchOutside(false);
            mProgressDialog.setMessage(message);
        }
        mProgressDialog.show();
    }

    /**
     * 显示ProgressDialog
     *
     * @param message
     */
    public void showProgress(String message) {
        if (mProgressDialog == null) {
            Drawable drawable = getResources().getDrawable(R.drawable.pic_progressbar_bg);
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setIndeterminateDrawable(drawable);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            mProgressDialog.setMessage(message);
        }
        mProgressDialog.show();
    }


    /**
     * dismiss ProgressDialog
     */
    public void hideProgress() {
        if (mProgressDialog == null)
            return;

        if (mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    /**
     * 显示toast
     *
     * @param tip
     */
    public void showToast(String tip) {
        ToastUtil.generalToast(this, tip);
    }

    /**
     * Activity跳转
     *
     * @param clazz
     */
    protected void readyGo(Class<?> clazz) {
        this.readyGo(clazz, (Bundle) null);
    }

    /**
     * 含有Bundle的Activity跳转
     *
     * @param clazz
     * @param bundle
     */
    protected void readyGo(Class<?> clazz, Bundle bundle) {
        Intent intent = new Intent(this, clazz);
        if (null != bundle) {
            intent.putExtras(bundle);
        }

        this.startActivity(intent);
    }

    /**
     * Activity跳转并结束
     *
     * @param clazz
     */
    protected void readyGoThenKill(Class<?> clazz) {
        this.readyGoThenKill(clazz, (Bundle) null);
    }

    /**
     * Activity 跳转并结束，同时传递Bundel
     *
     * @param clazz
     * @param bundle
     */
    protected void readyGoThenKill(Class<?> clazz, Bundle bundle) {
        this.readyGo(clazz, bundle);
        this.finish();
    }

    /**
     * ActivityForResult跳转
     *
     * @param clazz
     * @param requestCode
     */
    protected void readyGoForResult(Class<?> clazz, int requestCode) {
        Intent intent = new Intent(this, clazz);
        this.startActivityForResult(intent, requestCode);
    }

    /**
     * ActivityForResult跳转，附加Bundel
     *
     * @param clazz
     * @param requestCode
     * @param bundle
     */
    protected void readyGoForResult(Class<?> clazz, int requestCode, Bundle bundle) {
        Intent intent = new Intent(this, clazz);
        if (null != bundle) {
            intent.putExtras(bundle);
        }

        this.startActivityForResult(intent, requestCode);
    }

    /**
     * 加载过渡动画
     */
    protected void setupWindowAnimation() {
        FadeAnimation();
    }

    private static final int SLIDE_TRANSITION_TIME = 1 * 1000;//滑动转场的时间
    public Fade mFadeTransition;

    /**
     * 淡入淡出动画
     */
    public void FadeAnimation() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mFadeTransition = new Fade();
            mFadeTransition.setDuration(SLIDE_TRANSITION_TIME);
            getWindow().setEnterTransition(mFadeTransition);
            getWindow().setExitTransition(mFadeTransition);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterNetWorkReceiver();
        EventBus.getDefault().unregister(this);
    }


    /**
     * 无网
     */
    public void withOutNetWork() {

    }

    /**
     * 有移动网络网
     */
    public void withMobileNetWork() {
    }

    /**
     * 有wifi网
     */
    public void withWiFiNetWork() {

    }

    //注册监听网络变化
    private void registerNetWorkReceiver() {
        IntentFilter timeFilter = new IntentFilter();
        timeFilter.addAction("android.net.ethernet.ETHERNET_STATE_CHANGED");
        timeFilter.addAction("android.net.ethernet.STATE_CHANGE");
        timeFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        timeFilter.addAction("android.net.wifi.WIFI_STATE_CHANGED");
        timeFilter.addAction("android.net.wifi.STATE_CHANGE");
        timeFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        registerReceiver(netReceiver, timeFilter);

    }

    //取消监听网络变化
    private void unregisterNetWorkReceiver() {
        if (netReceiver != null) {
            unregisterReceiver(netReceiver);
            netReceiver = null;
        }
    }

}
